//
// Interface of a class to access data (virtual file system)
//
#include "Data.h"
#include "FileSystem.h"

// simple check whether the file exists
bool    fileExist ( const char * fileName )
{
return true;
}
							// build filename from path and a name
std::string		buildFileName ( const std::string& path, const std::string& name )
{
#ifdef	_WIN32
	char	pathSeparator = '\\';
#else
	char	pathSeparator = '/';
#endif

	if ( path.empty () )
		return name;

	int	pos = (int)path.length () - 1;

	std::string	res ( path );

	if ( path [pos] == '\\' || path [pos] == '/' || path [pos] == ':' )
		res += name;
	else
	{
		res += pathSeparator;
		res += name;
	}

	for ( int i = 0; i < (int)res.length (); i++ )
		if ( res [i] == '/' )
			res [i] = pathSeparator;

	return res;

}
								// extract path from a filename
std::string	getPath ( const std::string& fullName )
{
	for ( int i = (int)fullName.length () - 1; i >= 0; i-- )
	{
		char	ch = fullName [i];

		if ( ch == '\\' || ch == '/' || ch == ':' )
			return fullName.substr ( 0, i + 1 );
	}

	return "";

}
							// extract filename and extension
std::string	getFileName ( const std::string& fullName )
{
	int	len = (int)fullName.length ();

	for ( int i = len - 1; i >= 0; i-- )
	{
		char	ch = fullName [i];

		if ( ch == '\\' || ch == '/' || ch == ':' )
			return fullName.substr ( i + 1 );
	}

	return fullName;
}

// get only name without extension
std::string	getName ( const std::string& fullName )
{
	std::string	name = getFileName ( fullName );
	size_t	pos  = name.find ( '.' );

	if ( pos == std::string :: npos )
		return name;

	return name.substr ( 0, pos );
}

Folder :: Folder ( const std::string& theFolder ) : FileSystem (), folder ( theFolder ) 
{

}

Data  * Folder :: getFile  ( const std::string& name )
{
	std::string	newName = buildFileName ( folder, name );

	return new Data ( newName.c_str () );
}

Data * getFile ( const char * fileName )
{
	//initLibTexture ();								// it's safe to place it anywhere

    if ( fileExist ( fileName ) )
        return new Data ( fileName );
/*
    Data * data;

    for ( list <FileSystem *> :: iterator it = fileSystems.begin (); it != fileSystems.end (); ++it )
        if ( ( data = (*it) -> getFile ( fileName ) ) != NULL )
            return data;
*/
    return NULL;
}